<template>
  <view>
    <!-- 搜索 -->
    <view class="search-box">
      <my-search @click="gotoSearch"></my-search>
    </view>
    
    <!-- 轮播图区域 -->
    <swiper circular="true" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <!-- 循环渲染轮播图的 item 项 -->
      <swiper-item v-for="(item, i) in swiperList" :key="i">
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
          <!-- 动态绑定图片的 src 属性 -->
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, i) in navList" :key="i" @click="navClickHandler(item)">
        <image class="nav-img" :src="item.image_src"></image>
      </view>
    </view>
    
    <!-- 楼层区域 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, i) in floorList" :key="i">
        <!-- 楼层标题 -->
        <view class="floor-title">
          <image :src="item.floor_title.image_src"></image>
        </view>
        <!-- 楼层图片 -->
        <view class="floor-img">
          <!-- 左边图片 -->
          <navigator class="floor-img-left" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
          </navigator>
          
          <!-- 右边图片 -->
          <view class="floor-img-right">
            <navigator class="floor-img-right-item" :url="item2.url" v-for="(item2,i2) in item.product_list" :key="i2" v-if="i2 !== 0">
              <image :src="item2.image_src" :style="{width: item2.image_width + 'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
    
  </view>
</template>

<script>
  import setTabBar from '@/mixin/setTabBar.js'
  export default {
    mixins: [setTabBar],
    data() {
      return {
        swiperList: [], // 轮播图数据
        navList: [], // 导航数据
        floorList: [] // 楼层数据
      };
    },

    onLoad() {
      // 发请求
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },

    methods: {
      // 获取轮播图
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/home/swiperdata')
        // console.log(message)
        // 请求失败
        if (res.meta.status !== 200) {
          uni.showToast({
            duration: 1000,
            title: '网络请求错误',
            icon: 'none'
          })
        }

        // 请求成功
        this.swiperList = res.message
      },

      // 获取导航
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/home/catitems')

        // 请求失败
        if (res.meta.status !== 200) return uni.$showMsg()

        // 请求成功
        this.navList = res.message
      },
    
      // 点击分类跳转
      navClickHandler(item) {
        if(item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
    
    
      // 获取楼层数据
      async getFloorList() {
        const {data: res} = await uni.$http.get('/home/floordata')
        
        // 请求失败
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 修改数据格式
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            // console.log(prod)
            prod.url = `/subpkg/goods_list/goods_list?${prod.navigator_url.split('?')[1]}`
          })
        })
        
        // 请求成功 - 数据赋值
        this.floorList = res.message
      },
      
      // 点击搜索, 跳转到搜索页
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    },
    
  }
</script>

<style lang="scss">
  // 搜索
  .search-box {
    position: sticky;
    top: 0;
    left: 0;
    z-index: 9999;
  }
  
  
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 32rpx 0;
    image {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor-list {
    .floor-item {
      .floor-title image{
        width: 100%;
        height: 60rpx;
      }
      
      .floor-img {
        display: flex;
        margin-left: 8rpx;
        .floor-img-right {
          display: flex;
          flex-wrap: wrap;
          justify-content: space-around;
        }
      }
    }
  }


</style>
